package com.yuanduo_app.http;

import com.yuanduo_app.manager.CacheManager;
import com.xujl.fastlib.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

import okhttp3.RequestBody;

public class RequestCommonParams {

    public static final Map<String, String> createHeaders () {
        Map<String, String> headers = new HashMap<>();
        String token = CacheManager.getInstance().getToken();
        if (!StringUtils.isEmpty(token)) {
            headers.put("Authorization", token);
        }
        headers.put("Charset", "UTF-8");
        headers.put("Accept-Language", "zh-CN, zh;q=0.8, en-US;q=0.5, en;q=0.3");
        return headers;
    }


    public static Builder createParamBuilder () {
        return new Builder();
    }

    private RequestCommonParams () {
    }


    public static final class Builder {
        private final Map<String, Object> map = new HashMap<>();


        public final Builder clear () {
            this.map.clear();
            return this;
        }


        public final Builder addParams (String key, Object value) {
            if (value != null && key != null) {
                map.put(key, value);
                return this;
            } else {
                return this;
            }
        }

        public final Object getParam (String key) {
            return map.get(key);
        }

        public final Map<String, Object> build () {
            return this.map;
        }

        public RequestBody body () {
            return DataSource.createBody(map);
        }

        public final Builder remove (String key) {
            if (this.map.containsKey(key)) {
                this.map.remove(key);
            }
            return this;
        }
    }
}
